<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>iframepollingtransport.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_xpc_iframepollingtransport.js.html">iframepollingtransport.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Contains the iframe polling transport.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.net.xpc.IframePollingTransport&#39;);
<a name="line21"></a>goog.provide(&#39;goog.net.xpc.IframePollingTransport.Receiver&#39;);
<a name="line22"></a>goog.provide(&#39;goog.net.xpc.IframePollingTransport.Sender&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.array&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom&#39;);
<a name="line26"></a>goog.require(&#39;goog.net.xpc&#39;);
<a name="line27"></a>goog.require(&#39;goog.net.xpc.Transport&#39;);
<a name="line28"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Iframe polling transport. Uses hidden iframes to transfer data
<a name="line33"></a> * in the fragment identifier of the URL. The peer polls the iframe&#39;s location
<a name="line34"></a> * for changes.
<a name="line35"></a> * Unfortunately, in Safari this screws up the history, because Safari doesn&#39;t
<a name="line36"></a> * allow to call location.replace() on a window containing a document from a
<a name="line37"></a> * different domain (last version tested: 2.0.4).
<a name="line38"></a> *
<a name="line39"></a> * @param {goog.net.xpc.CrossPageChannel} channel The channel this
<a name="line40"></a> *     transport belongs to.
<a name="line41"></a> * @constructor
<a name="line42"></a> * @extends {goog.net.xpc.Transport}
<a name="line43"></a> */
<a name="line44"></a>goog.net.xpc.IframePollingTransport = function(channel) {
<a name="line45"></a>
<a name="line46"></a>  /**
<a name="line47"></a>   * The channel this transport belongs to.
<a name="line48"></a>   * @type {goog.net.xpc.CrossPageChannel}
<a name="line49"></a>   * @private
<a name="line50"></a>   */
<a name="line51"></a>  this.channel_ = channel;
<a name="line52"></a>
<a name="line53"></a>  /**
<a name="line54"></a>   * The URI used to send messages.
<a name="line55"></a>   * @type {string}
<a name="line56"></a>   * @private
<a name="line57"></a>   */
<a name="line58"></a>  this.sendUri_ = this.channel_.cfg_[goog.net.xpc.CfgFields.PEER_POLL_URI];
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * The URI which is polled for incoming messages.
<a name="line62"></a>   * @type {string}
<a name="line63"></a>   * @private
<a name="line64"></a>   */
<a name="line65"></a>  this.rcvUri_ = this.channel_.cfg_[goog.net.xpc.CfgFields.LOCAL_POLL_URI];
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * The queue to hold messages which can&#39;t be sent immediately.
<a name="line69"></a>   * @type {Array}
<a name="line70"></a>   * @private
<a name="line71"></a>   */
<a name="line72"></a>  this.sendQueue_ = [];
<a name="line73"></a>};
<a name="line74"></a>goog.inherits(goog.net.xpc.IframePollingTransport, goog.net.xpc.Transport);
<a name="line75"></a>
<a name="line76"></a>
<a name="line77"></a>/**
<a name="line78"></a> * The transport type.
<a name="line79"></a> * @type {number}
<a name="line80"></a> * @protected
<a name="line81"></a> */
<a name="line82"></a>goog.net.xpc.IframePollingTransport.prototype.transportType =
<a name="line83"></a>  goog.net.xpc.TransportTypes.IFRAME_POLLING;
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/**
<a name="line87"></a> * Sequence counter.
<a name="line88"></a> * @type {number}
<a name="line89"></a> * @private
<a name="line90"></a> */
<a name="line91"></a>goog.net.xpc.IframePollingTransport.prototype.sequence_ = 0;
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Flag indicating whether we are waiting for an acknoledgement.
<a name="line96"></a> * @type {boolean}
<a name="line97"></a> * @private
<a name="line98"></a> */
<a name="line99"></a>goog.net.xpc.IframePollingTransport.prototype.waitForAck_ = false;
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Flag indicating if channel has been initialized.
<a name="line104"></a> * @type {boolean}
<a name="line105"></a> * @private
<a name="line106"></a> */
<a name="line107"></a>goog.net.xpc.IframePollingTransport.prototype.initialized_ = false;
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>/**
<a name="line111"></a> * The string used to prefix all iframe names and IDs.
<a name="line112"></a> * @type {string}
<a name="line113"></a> */
<a name="line114"></a>goog.net.xpc.IframePollingTransport.IFRAME_PREFIX = &#39;googlexpc&#39;;
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Returns the name/ID of the message frame.
<a name="line119"></a> * @return {string} Name of message frame.
<a name="line120"></a> * @private
<a name="line121"></a> */
<a name="line122"></a>goog.net.xpc.IframePollingTransport.prototype.getMsgFrameName_ = function() {
<a name="line123"></a>  return goog.net.xpc.IframePollingTransport.IFRAME_PREFIX + &#39;_&#39; +
<a name="line124"></a>      this.channel_.name + &#39;_msg&#39;;
<a name="line125"></a>};
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * Returns the name/ID of the ack frame.
<a name="line130"></a> * @return {string} Name of ack frame.
<a name="line131"></a> * @private
<a name="line132"></a> */
<a name="line133"></a>goog.net.xpc.IframePollingTransport.prototype.getAckFrameName_ = function() {
<a name="line134"></a>  return goog.net.xpc.IframePollingTransport.IFRAME_PREFIX + &#39;_&#39; +
<a name="line135"></a>      this.channel_.name + &#39;_ack&#39;;
<a name="line136"></a>};
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * Connects this transport.
<a name="line141"></a> */
<a name="line142"></a>goog.net.xpc.IframePollingTransport.prototype.connect = function() {
<a name="line143"></a>  goog.net.xpc.logger.fine(&#39;transport connect called&#39;);
<a name="line144"></a>  if (!this.initialized_) {
<a name="line145"></a>    goog.net.xpc.logger.fine(&#39;initializing...&#39;);
<a name="line146"></a>    this.constructSenderFrames_();
<a name="line147"></a>    this.initialized_ = true;
<a name="line148"></a>  }
<a name="line149"></a>  this.checkForeignFramesReady_();
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * Creates the iframes which are used to send messages (and acknowledgements)
<a name="line155"></a> * to the peer. Sender iframes contain a document from a different origin and
<a name="line156"></a> * therefore their content can&#39;t be accessed.
<a name="line157"></a> * @private
<a name="line158"></a> */
<a name="line159"></a>goog.net.xpc.IframePollingTransport.prototype.constructSenderFrames_ =
<a name="line160"></a>    function() {
<a name="line161"></a>  var name = this.getMsgFrameName_();
<a name="line162"></a>  this.msgIframeElm_ = this.constructSenderFrame_(name);
<a name="line163"></a>  this.msgWinObj_ = window.frames[name];
<a name="line164"></a>
<a name="line165"></a>  name = this.getAckFrameName_();
<a name="line166"></a>  this.ackIframeElm_ = this.constructSenderFrame_(name);
<a name="line167"></a>  this.ackWinObj_ = window.frames[name];
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * Constructs a sending frame the the given id.
<a name="line173"></a> * @param {string} id The id.
<a name="line174"></a> * @return {Element} The constructed frame.
<a name="line175"></a> * @private
<a name="line176"></a> */
<a name="line177"></a>goog.net.xpc.IframePollingTransport.prototype.constructSenderFrame_ =
<a name="line178"></a>    function(id) {
<a name="line179"></a>  goog.net.xpc.logger.finest(&#39;constructing sender frame: &#39; + id);
<a name="line180"></a>  var ifr = goog.dom.createElement(&#39;iframe&#39;);
<a name="line181"></a>  var s = ifr.style;
<a name="line182"></a>  s.position = &#39;absolute&#39;;
<a name="line183"></a>  s.top = &#39;-10px&#39;; s.left = &#39;10px&#39;; s.width = &#39;1px&#39;; s.height = &#39;1px&#39;;
<a name="line184"></a>  ifr.id = ifr.name = id;
<a name="line185"></a>  ifr.src = this.sendUri_ + &#39;#INITIAL&#39;;
<a name="line186"></a>  document.body.appendChild(ifr);
<a name="line187"></a>  return ifr;
<a name="line188"></a>};
<a name="line189"></a>
<a name="line190"></a>
<a name="line191"></a>/**
<a name="line192"></a> * The protocol for reconnecting is for the inner frame to change channel
<a name="line193"></a> * names, and then communicate the new channel name to the outer peer.
<a name="line194"></a> * The outer peer looks in a predefined location for the channel name
<a name="line195"></a> * upate. It is important to use a completely new channel name, as this
<a name="line196"></a> * will ensure that all messaging iframes are not in the bfcache.
<a name="line197"></a> * Otherwise, Safari may pollute the history when modifying the location
<a name="line198"></a> * of bfcached iframes.
<a name="line199"></a> * @private
<a name="line200"></a> */
<a name="line201"></a>goog.net.xpc.IframePollingTransport.prototype.innerPeerReconnect_ = function() {
<a name="line202"></a>  goog.net.xpc.logger.finest(&#39;innerPeerReconnect called&#39;);
<a name="line203"></a>  this.channel_.name = goog.net.xpc.getRandomString(10);
<a name="line204"></a>  goog.net.xpc.logger.finest(&#39;switching channels: &#39; + this.channel_.name);
<a name="line205"></a>  this.deconstructSenderFrames_();
<a name="line206"></a>  this.initialized_ = false;
<a name="line207"></a>  // Communicate new channel name to outer peer.
<a name="line208"></a>  this.reconnectFrame_ = this.constructSenderFrame_(
<a name="line209"></a>    goog.net.xpc.IframePollingTransport.IFRAME_PREFIX +
<a name="line210"></a>      &#39;_reconnect_&#39; + this.channel_.name);
<a name="line211"></a>};
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/**
<a name="line215"></a> * Scans inner peer for a reconnect message, which will be used to update
<a name="line216"></a> * the outer peer&#39;s channel name. If a reconnect message is found, the
<a name="line217"></a> * sender frames will be cleaned up to make way for the new sender frames.
<a name="line218"></a> * Only called by the outer peer.
<a name="line219"></a> * @private
<a name="line220"></a> */
<a name="line221"></a>goog.net.xpc.IframePollingTransport.prototype.outerPeerReconnect_ = function() {
<a name="line222"></a>  goog.net.xpc.logger.finest(&#39;outerPeerReconnect called&#39;);
<a name="line223"></a>  var frames = this.channel_.peerWindowObject_.frames;
<a name="line224"></a>  var length = frames.length;
<a name="line225"></a>  for (var i = 0; i &lt; length; i++) {
<a name="line226"></a>    var frameName;
<a name="line227"></a>    try {
<a name="line228"></a>      if (frames[i] &amp;&amp; frames[i].name) {
<a name="line229"></a>        frameName = frames[i].name;
<a name="line230"></a>      }
<a name="line231"></a>    } catch (e) {
<a name="line232"></a>      // Do nothing.
<a name="line233"></a>    }
<a name="line234"></a>    if (!frameName) {
<a name="line235"></a>      continue;
<a name="line236"></a>    }
<a name="line237"></a>    var message = frameName.split(&#39;_&#39;);
<a name="line238"></a>    if (message.length == 3 &amp;&amp;
<a name="line239"></a>        message[0] == goog.net.xpc.IframePollingTransport.IFRAME_PREFIX &amp;&amp;
<a name="line240"></a>        message[1] == &#39;reconnect&#39;) {
<a name="line241"></a>      // This is a legitimate reconnect message from the peer. Start using
<a name="line242"></a>      // the peer provided channel name, and start a connection over from
<a name="line243"></a>      // scratch.
<a name="line244"></a>      this.channel_.name = message[2];
<a name="line245"></a>      this.deconstructSenderFrames_();
<a name="line246"></a>      this.initialized_ = false;
<a name="line247"></a>      break;
<a name="line248"></a>    }
<a name="line249"></a>  }
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/**
<a name="line254"></a> * Cleans up the existing sender frames owned by this peer. Only called by
<a name="line255"></a> * the outer peer.
<a name="line256"></a> * @private
<a name="line257"></a> */
<a name="line258"></a>goog.net.xpc.IframePollingTransport.prototype.deconstructSenderFrames_ =
<a name="line259"></a>    function() {
<a name="line260"></a>  goog.net.xpc.logger.finest(&#39;deconstructSenderFrames called&#39;);
<a name="line261"></a>  if (this.msgIframeElm_) {
<a name="line262"></a>    this.msgIframeElm_.parentNode.removeChild(this.msgIframeElm_);
<a name="line263"></a>    this.msgIframeElm_ = null;
<a name="line264"></a>    this.msgWinObj_ = null;
<a name="line265"></a>  }
<a name="line266"></a>  if (this.ackIframeElm_) {
<a name="line267"></a>    this.ackIframeElm_.parentNode.removeChild(this.ackIframeElm_);
<a name="line268"></a>    this.ackIframeElm_ = null;
<a name="line269"></a>    this.ackWinObj_ = null;
<a name="line270"></a>  }
<a name="line271"></a>};
<a name="line272"></a>
<a name="line273"></a>
<a name="line274"></a>/**
<a name="line275"></a> * Checks if the frames in the peer&#39;s page are ready. These contain a
<a name="line276"></a> * document from the own domain and are the ones messages are received through.
<a name="line277"></a> * @private
<a name="line278"></a> */
<a name="line279"></a>goog.net.xpc.IframePollingTransport.prototype.checkForeignFramesReady_ =
<a name="line280"></a>    function() {
<a name="line281"></a>  // check if the connected iframe ready
<a name="line282"></a>  if (!(this.isRcvFrameReady_(this.getMsgFrameName_()) &amp;&amp;
<a name="line283"></a>        this.isRcvFrameReady_(this.getAckFrameName_()))) {
<a name="line284"></a>    goog.net.xpc.logger.finest(&#39;foreign frames not (yet) present&#39;);
<a name="line285"></a>
<a name="line286"></a>    if (this.channel_.getRole() == goog.net.xpc.CrossPageChannel.Role.INNER &amp;&amp;
<a name="line287"></a>        !this.reconnectFrame_) {
<a name="line288"></a>      // The inner peer should always have its receiving frames ready.
<a name="line289"></a>      // It is safe to assume the channel name has fallen out of sync
<a name="line290"></a>      // (which happens with bfcached frames). Create &quot;reconnect&quot; frames,
<a name="line291"></a>      // which the outer peer will find, and use to resync the channel names.
<a name="line292"></a>      this.innerPeerReconnect_();
<a name="line293"></a>    } else if (this.channel_.getRole() ==
<a name="line294"></a>               goog.net.xpc.CrossPageChannel.Role.OUTER) {
<a name="line295"></a>      // The inner peer is either not loaded yet, or the receiving
<a name="line296"></a>      // frames are simply missing. Since we cannot discern the two cases, we
<a name="line297"></a>      // should scan for a reconnect message from the inner peer.
<a name="line298"></a>      this.outerPeerReconnect_();
<a name="line299"></a>    }
<a name="line300"></a>
<a name="line301"></a>    // start a timer to check again
<a name="line302"></a>    window.setTimeout(goog.bind(this.connect, this), 100);
<a name="line303"></a>  } else {
<a name="line304"></a>    goog.net.xpc.logger.fine(&#39;foreign frames present&#39;);
<a name="line305"></a>
<a name="line306"></a>    // Create receivers.
<a name="line307"></a>    this.msgReceiver_ = new goog.net.xpc.IframePollingTransport.Receiver(
<a name="line308"></a>        this,
<a name="line309"></a>        this.channel_.peerWindowObject_.frames[this.getMsgFrameName_()],
<a name="line310"></a>        goog.bind(this.processIncomingMsg, this));
<a name="line311"></a>    this.ackReceiver_ = new goog.net.xpc.IframePollingTransport.Receiver(
<a name="line312"></a>        this,
<a name="line313"></a>        this.channel_.peerWindowObject_.frames[this.getAckFrameName_()],
<a name="line314"></a>        goog.bind(this.processIncomingAck, this));
<a name="line315"></a>
<a name="line316"></a>    this.checkLocalFramesPresent_();
<a name="line317"></a>  }
<a name="line318"></a>};
<a name="line319"></a>
<a name="line320"></a>
<a name="line321"></a>/**
<a name="line322"></a> * Checks if the receiving frame is ready.
<a name="line323"></a> * @param {string} frameName Which receiving frame to check.
<a name="line324"></a> * @return {boolean} Whether the receiving frame is ready.
<a name="line325"></a> * @private
<a name="line326"></a> */
<a name="line327"></a>goog.net.xpc.IframePollingTransport.prototype.isRcvFrameReady_ =
<a name="line328"></a>    function(frameName) {
<a name="line329"></a>  goog.net.xpc.logger.finest(&#39;checking for receive frame: &#39; + frameName);
<a name="line330"></a>  /** @preserveTry */
<a name="line331"></a>  try {
<a name="line332"></a>    var winObj = this.channel_.peerWindowObject_.frames[frameName];
<a name="line333"></a>    if (!winObj || winObj.location.href.indexOf(this.rcvUri_) != 0) {
<a name="line334"></a>      return false;
<a name="line335"></a>    }
<a name="line336"></a>  } catch (e) {
<a name="line337"></a>    return false;
<a name="line338"></a>  }
<a name="line339"></a>  return true;
<a name="line340"></a>};
<a name="line341"></a>
<a name="line342"></a>
<a name="line343"></a>/**
<a name="line344"></a> * Checks if the iframes created in the own document are ready.
<a name="line345"></a> * @private
<a name="line346"></a> */
<a name="line347"></a>goog.net.xpc.IframePollingTransport.prototype.checkLocalFramesPresent_ =
<a name="line348"></a>    function() {
<a name="line349"></a>
<a name="line350"></a>  // Are the sender frames ready?
<a name="line351"></a>  // These contain a document from the peer&#39;s domain, therefore we can only
<a name="line352"></a>  // check if the frame itself is present.
<a name="line353"></a>  var frames = this.channel_.peerWindowObject_.frames;
<a name="line354"></a>  if (!(frames[this.getAckFrameName_()] &amp;&amp;
<a name="line355"></a>        frames[this.getMsgFrameName_()])) {
<a name="line356"></a>    // start a timer to check again
<a name="line357"></a>    if (!this.checkLocalFramesPresentCb_) {
<a name="line358"></a>      this.checkLocalFramesPresentCb_ = goog.bind(
<a name="line359"></a>          this.checkLocalFramesPresent_, this);
<a name="line360"></a>    }
<a name="line361"></a>    window.setTimeout(this.checkLocalFramesPresentCb_, 100);
<a name="line362"></a>    goog.net.xpc.logger.fine(&#39;local frames not (yet) present&#39;);
<a name="line363"></a>  } else {
<a name="line364"></a>    // Create senders.
<a name="line365"></a>    this.msgSender_ = new goog.net.xpc.IframePollingTransport.Sender(
<a name="line366"></a>        this.sendUri_, this.msgWinObj_);
<a name="line367"></a>    this.ackSender_ = new goog.net.xpc.IframePollingTransport.Sender(
<a name="line368"></a>        this.sendUri_, this.ackWinObj_);
<a name="line369"></a>
<a name="line370"></a>    goog.net.xpc.logger.fine(&#39;local frames ready&#39;);
<a name="line371"></a>
<a name="line372"></a>    window.setTimeout(goog.bind(function() {
<a name="line373"></a>      this.msgSender_.send(goog.net.xpc.SETUP);
<a name="line374"></a>      this.sentConnectionSetup_ = true;
<a name="line375"></a>      this.waitForAck_ = true;
<a name="line376"></a>      goog.net.xpc.logger.fine(&#39;SETUP sent&#39;);
<a name="line377"></a>    }, this), 100);
<a name="line378"></a>  }
<a name="line379"></a>};
<a name="line380"></a>
<a name="line381"></a>
<a name="line382"></a>/**
<a name="line383"></a> * Check if connection is ready.
<a name="line384"></a> * @private
<a name="line385"></a> */
<a name="line386"></a>goog.net.xpc.IframePollingTransport.prototype.checkIfConnected_ = function() {
<a name="line387"></a>  if (this.sentConnectionSetupAck_ &amp;&amp; this.rcvdConnectionSetupAck_) {
<a name="line388"></a>    this.channel_.notifyConnected_();
<a name="line389"></a>
<a name="line390"></a>    if (this.deliveryQueue_) {
<a name="line391"></a>      goog.net.xpc.logger.fine(&#39;delivering queued messages &#39; +
<a name="line392"></a>                               &#39;(&#39; + this.deliveryQueue_.length + &#39;)&#39;);
<a name="line393"></a>
<a name="line394"></a>      for (var i = 0, m; i &lt; this.deliveryQueue_.length; i++) {
<a name="line395"></a>        m = this.deliveryQueue_[i];
<a name="line396"></a>        this.channel_.deliver_(m.service, m.payload);
<a name="line397"></a>      }
<a name="line398"></a>      delete this.deliveryQueue_;
<a name="line399"></a>    }
<a name="line400"></a>  } else {
<a name="line401"></a>    goog.net.xpc.logger.finest(&#39;checking if connected: &#39; +
<a name="line402"></a>                               &#39;ack sent:&#39; + this.sentConnectionSetupAck_ +
<a name="line403"></a>                               &#39;, ack rcvd: &#39; + this.rcvdConnectionSetupAck_);
<a name="line404"></a>  }
<a name="line405"></a>};
<a name="line406"></a>
<a name="line407"></a>
<a name="line408"></a>/**
<a name="line409"></a> * Processes an incoming message.
<a name="line410"></a> * @param {string} raw The complete received string.
<a name="line411"></a> */
<a name="line412"></a>goog.net.xpc.IframePollingTransport.prototype.processIncomingMsg =
<a name="line413"></a>    function(raw) {
<a name="line414"></a>  goog.net.xpc.logger.finest(&#39;msg received: &#39; + raw);
<a name="line415"></a>
<a name="line416"></a>  if (raw == goog.net.xpc.SETUP) {
<a name="line417"></a>    if (!this.ackSender_) {
<a name="line418"></a>      // Got SETUP msg, but we can&#39;t send an ack.
<a name="line419"></a>      return;
<a name="line420"></a>    }
<a name="line421"></a>
<a name="line422"></a>    this.ackSender_.send(goog.net.xpc.SETUP_ACK_);
<a name="line423"></a>    goog.net.xpc.logger.finest(&#39;SETUP_ACK sent&#39;);
<a name="line424"></a>
<a name="line425"></a>    this.sentConnectionSetupAck_ = true;
<a name="line426"></a>    this.checkIfConnected_();
<a name="line427"></a>
<a name="line428"></a>  } else if (this.channel_.isConnected() || this.sentConnectionSetupAck_) {
<a name="line429"></a>
<a name="line430"></a>    var pos = raw.indexOf(&#39;|&#39;);
<a name="line431"></a>    var head = raw.substring(0, pos);
<a name="line432"></a>    var frame = raw.substring(pos + 1);
<a name="line433"></a>
<a name="line434"></a>    // check if it is a framed message
<a name="line435"></a>    pos = head.indexOf(&#39;,&#39;);
<a name="line436"></a>    if (pos == -1) {
<a name="line437"></a>      var seq = head;
<a name="line438"></a>      // send acknowledgement
<a name="line439"></a>      this.ackSender_.send(&#39;ACK:&#39; + seq);
<a name="line440"></a>      this.deliverPayload_(frame);
<a name="line441"></a>    } else {
<a name="line442"></a>      var seq = head.substring(0, pos);
<a name="line443"></a>      // send acknowledgement
<a name="line444"></a>      this.ackSender_.send(&#39;ACK:&#39; + seq);
<a name="line445"></a>
<a name="line446"></a>      var partInfo = head.substring(pos + 1).split(&#39;/&#39;);
<a name="line447"></a>      partInfo[0] = parseInt(partInfo[0], 10);
<a name="line448"></a>      partInfo[1] = parseInt(partInfo[1], 10);
<a name="line449"></a>      // create an array to accumulate the parts if this is the
<a name="line450"></a>      // first frame of a message
<a name="line451"></a>      if (partInfo[0] == 1) {
<a name="line452"></a>        this.parts_ = [];
<a name="line453"></a>      }
<a name="line454"></a>      this.parts_.push(frame);
<a name="line455"></a>      // deliver the message if this was the last frame of a message
<a name="line456"></a>      if (partInfo[0] == partInfo[1]) {
<a name="line457"></a>        this.deliverPayload_(this.parts_.join(&#39;&#39;));
<a name="line458"></a>        delete this.parts_;
<a name="line459"></a>      }
<a name="line460"></a>    }
<a name="line461"></a>  } else {
<a name="line462"></a>    goog.net.xpc.logger.warning(&#39;received msg, but channel is not connected&#39;);
<a name="line463"></a>  }
<a name="line464"></a>};
<a name="line465"></a>
<a name="line466"></a>
<a name="line467"></a>/**
<a name="line468"></a> * Process an incoming acknowdedgement.
<a name="line469"></a> * @param {string} msgStr The incoming ack string to process.
<a name="line470"></a> */
<a name="line471"></a>goog.net.xpc.IframePollingTransport.prototype.processIncomingAck =
<a name="line472"></a>    function(msgStr) {
<a name="line473"></a>  goog.net.xpc.logger.finest(&#39;ack received: &#39; + msgStr);
<a name="line474"></a>
<a name="line475"></a>  if (msgStr == goog.net.xpc.SETUP_ACK_) {
<a name="line476"></a>    this.waitForAck_ = false;
<a name="line477"></a>    this.rcvdConnectionSetupAck_ = true;
<a name="line478"></a>    // send the next frame
<a name="line479"></a>    this.checkIfConnected_();
<a name="line480"></a>
<a name="line481"></a>  } else if (this.channel_.isConnected()) {
<a name="line482"></a>    if (!this.waitForAck_) {
<a name="line483"></a>      goog.net.xpc.logger.warning(&#39;got unexpected ack&#39;);
<a name="line484"></a>      return;
<a name="line485"></a>    }
<a name="line486"></a>
<a name="line487"></a>    var seq = parseInt(msgStr.split(&#39;:&#39;)[1], 10);
<a name="line488"></a>    if (seq == this.sequence_) {
<a name="line489"></a>      this.waitForAck_ = false;
<a name="line490"></a>      this.sendNextFrame_();
<a name="line491"></a>    } else {
<a name="line492"></a>      goog.net.xpc.logger.warning(&#39;got ack with wrong sequence&#39;);
<a name="line493"></a>    }
<a name="line494"></a>  } else {
<a name="line495"></a>    goog.net.xpc.logger.warning(&#39;received ack, but channel not connected&#39;);
<a name="line496"></a>  }
<a name="line497"></a>};
<a name="line498"></a>
<a name="line499"></a>
<a name="line500"></a>/**
<a name="line501"></a> * Sends a frame (message part).
<a name="line502"></a> * @private
<a name="line503"></a> */
<a name="line504"></a>goog.net.xpc.IframePollingTransport.prototype.sendNextFrame_ = function() {
<a name="line505"></a>  // do nothing if we are waiting for an acknowledgement or the
<a name="line506"></a>  // queue is emtpy
<a name="line507"></a>  if (this.waitForAck_ || !this.sendQueue_.length) {
<a name="line508"></a>    return;
<a name="line509"></a>  }
<a name="line510"></a>
<a name="line511"></a>  var s = this.sendQueue_.shift();
<a name="line512"></a>  ++this.sequence_;
<a name="line513"></a>  this.msgSender_.send(this.sequence_ + s);
<a name="line514"></a>  goog.net.xpc.logger.finest(&#39;msg sent: &#39; + this.sequence_ + s);
<a name="line515"></a>
<a name="line516"></a>
<a name="line517"></a>  this.waitForAck_ = true;
<a name="line518"></a>};
<a name="line519"></a>
<a name="line520"></a>
<a name="line521"></a>/**
<a name="line522"></a> * Delivers a message.
<a name="line523"></a> * @param {string} s The complete message string (&quot;&lt;service_name&gt;:&lt;payload&gt;&quot;).
<a name="line524"></a> * @private
<a name="line525"></a> */
<a name="line526"></a>goog.net.xpc.IframePollingTransport.prototype.deliverPayload_ = function(s) {
<a name="line527"></a>  // determine the service name and the payload
<a name="line528"></a>  var pos = s.indexOf(&#39;:&#39;);
<a name="line529"></a>  var service = s.substr(0, pos);
<a name="line530"></a>  var payload = s.substring(pos + 1);
<a name="line531"></a>
<a name="line532"></a>  // deliver the message
<a name="line533"></a>  if (!this.channel_.isConnected()) {
<a name="line534"></a>    // as valid messages can come in before a SETUP_ACK has
<a name="line535"></a>    // been received (because subchannels for msgs and acks are independent),
<a name="line536"></a>    // delay delivery of early messages until after &#39;connect&#39;-event
<a name="line537"></a>    (this.deliveryQueue_ || (this.deliveryQueue_ = [])).
<a name="line538"></a>        push({service: service, payload: payload});
<a name="line539"></a>    goog.net.xpc.logger.finest(&#39;queued delivery&#39;);
<a name="line540"></a>  } else {
<a name="line541"></a>    this.channel_.deliver_(service, payload);
<a name="line542"></a>  }
<a name="line543"></a>};
<a name="line544"></a>
<a name="line545"></a>
<a name="line546"></a>// ---- send message ----
<a name="line547"></a>
<a name="line548"></a>
<a name="line549"></a>/**
<a name="line550"></a> * Maximal frame length.
<a name="line551"></a> * @type {number}
<a name="line552"></a> * @private
<a name="line553"></a> */
<a name="line554"></a>goog.net.xpc.IframePollingTransport.prototype.MAX_FRAME_LENGTH_ = 3800;
<a name="line555"></a>
<a name="line556"></a>
<a name="line557"></a>/**
<a name="line558"></a> * Sends a message. Splits it in multiple frames if too long (exceeds IE&#39;s
<a name="line559"></a> * URL-length maximum.
<a name="line560"></a> * Wireformat: &lt;seq&gt;[,&lt;frame_no&gt;/&lt;#frames&gt;]|&lt;frame_content&gt;
<a name="line561"></a> *
<a name="line562"></a> * @param {string} service Name of service this the message has to be delivered.
<a name="line563"></a> * @param {string} payload The message content.
<a name="line564"></a> */
<a name="line565"></a>goog.net.xpc.IframePollingTransport.prototype.send =
<a name="line566"></a>    function(service, payload) {
<a name="line567"></a>  var frame = service + &#39;:&#39; + payload;
<a name="line568"></a>  // put in queue
<a name="line569"></a>  if (!goog.userAgent.IE || payload.length &lt;= this.MAX_FRAME_LENGTH_) {
<a name="line570"></a>    this.sendQueue_.push(&#39;|&#39; + frame);
<a name="line571"></a>  }
<a name="line572"></a>  else {
<a name="line573"></a>    var l = payload.length;
<a name="line574"></a>    var num = Math.ceil(l / this.MAX_FRAME_LENGTH_); // number of frames
<a name="line575"></a>    var pos = 0;
<a name="line576"></a>    var i = 1;
<a name="line577"></a>    while (pos &lt; l) {
<a name="line578"></a>      this.sendQueue_.push(&#39;,&#39; + i + &#39;/&#39; + num + &#39;|&#39; +
<a name="line579"></a>                           frame.substr(pos, this.MAX_FRAME_LENGTH_));
<a name="line580"></a>      i++;
<a name="line581"></a>      pos += this.MAX_FRAME_LENGTH_;
<a name="line582"></a>    }
<a name="line583"></a>  }
<a name="line584"></a>  this.sendNextFrame_();
<a name="line585"></a>};
<a name="line586"></a>
<a name="line587"></a>
<a name="line588"></a>/**
<a name="line589"></a> * Disposes of the transport.
<a name="line590"></a> */
<a name="line591"></a>goog.net.xpc.IframePollingTransport.prototype.disposeInternal = function() {
<a name="line592"></a>  goog.net.xpc.IframePollingTransport.superClass_.disposeInternal.call(this);
<a name="line593"></a>
<a name="line594"></a>  var receivers = goog.net.xpc.IframePollingTransport.receivers_;
<a name="line595"></a>  goog.array.remove(receivers, this.msgReceiver_);
<a name="line596"></a>  goog.array.remove(receivers, this.ackReceiver_);
<a name="line597"></a>  this.msgReceiver_ = this.ackReceiver_ = null;
<a name="line598"></a>
<a name="line599"></a>  goog.dom.removeNode(this.msgIframeElm_);
<a name="line600"></a>  goog.dom.removeNode(this.ackIframeElm_);
<a name="line601"></a>  this.msgIframeElm_ = this.ackIframeElm_ = null;
<a name="line602"></a>  this.msgWinObj_ = this.ackWinObj_ = null;
<a name="line603"></a>};
<a name="line604"></a>
<a name="line605"></a>
<a name="line606"></a>/**
<a name="line607"></a> * Array holding all Receiver-instances.
<a name="line608"></a> * @type {Array.&lt;goog.net.xpc.IframePollingTransport.Receiver&gt;}
<a name="line609"></a> * @private
<a name="line610"></a> */
<a name="line611"></a>goog.net.xpc.IframePollingTransport.receivers_ = [];
<a name="line612"></a>
<a name="line613"></a>
<a name="line614"></a>/**
<a name="line615"></a> * Short polling interval.
<a name="line616"></a> * @type {number}
<a name="line617"></a> * @private
<a name="line618"></a> */
<a name="line619"></a>goog.net.xpc.IframePollingTransport.TIME_POLL_SHORT_ = 10;
<a name="line620"></a>
<a name="line621"></a>
<a name="line622"></a>/**
<a name="line623"></a> * Long polling interval.
<a name="line624"></a> * @type {number}
<a name="line625"></a> * @private
<a name="line626"></a> */
<a name="line627"></a>goog.net.xpc.IframePollingTransport.TIME_POLL_LONG_ = 100;
<a name="line628"></a>
<a name="line629"></a>
<a name="line630"></a>/**
<a name="line631"></a> * Period how long to use TIME_POLL_SHORT_ before raising polling-interval
<a name="line632"></a> * to TIME_POLL_LONG_ after an activity.
<a name="line633"></a> * @type {number}
<a name="line634"></a> * @private
<a name="line635"></a> */
<a name="line636"></a>goog.net.xpc.IframePollingTransport.TIME_SHORT_POLL_AFTER_ACTIVITY_ =
<a name="line637"></a>    1000;
<a name="line638"></a>
<a name="line639"></a>
<a name="line640"></a>/**
<a name="line641"></a> * Polls all receivers.
<a name="line642"></a> * @private
<a name="line643"></a> */
<a name="line644"></a>goog.net.xpc.IframePollingTransport.receive_ = function() {
<a name="line645"></a>  var rcvd = false;
<a name="line646"></a>  /** @preserveTry */
<a name="line647"></a>  try {
<a name="line648"></a>    for (var i = 0, l = goog.net.xpc.IframePollingTransport.receivers_.length;
<a name="line649"></a>         i &lt; l; i++) {
<a name="line650"></a>      rcvd = rcvd ||
<a name="line651"></a>          goog.net.xpc.IframePollingTransport.receivers_[i].receive();
<a name="line652"></a>    }
<a name="line653"></a>  } catch (e) {
<a name="line654"></a>    goog.net.xpc.logger.info(&#39;receive_() failed: &#39; + e);
<a name="line655"></a>    // Notify the channel that the transport had an error.
<a name="line656"></a>    goog.net.xpc.IframePollingTransport.receivers_[i].
<a name="line657"></a>        transport_.channel_.notifyTransportError_();
<a name="line658"></a>    // notifyTransportError_() closes the channel and dispoases the transport.
<a name="line659"></a>    // If there are no other channels present, this.receivers_ will now be empty
<a name="line660"></a>    // and there is not need to keep polling.
<a name="line661"></a>    if (!goog.net.xpc.IframePollingTransport.receivers_.length) {
<a name="line662"></a>      return;
<a name="line663"></a>    }
<a name="line664"></a>  }
<a name="line665"></a>
<a name="line666"></a>  var now = goog.now();
<a name="line667"></a>  if (rcvd) {
<a name="line668"></a>    goog.net.xpc.IframePollingTransport.lastActivity_ = now;
<a name="line669"></a>  }
<a name="line670"></a>
<a name="line671"></a>  // Schedule next check.
<a name="line672"></a>  var t = now - goog.net.xpc.IframePollingTransport.lastActivity_ &lt;
<a name="line673"></a>      goog.net.xpc.IframePollingTransport.TIME_SHORT_POLL_AFTER_ACTIVITY_ ?
<a name="line674"></a>      goog.net.xpc.IframePollingTransport.TIME_POLL_SHORT_ :
<a name="line675"></a>      goog.net.xpc.IframePollingTransport.TIME_POLL_LONG_;
<a name="line676"></a>  goog.net.xpc.IframePollingTransport.rcvTimer_ = window.setTimeout(
<a name="line677"></a>      goog.net.xpc.IframePollingTransport.receiveCb_, t);
<a name="line678"></a>};
<a name="line679"></a>
<a name="line680"></a>
<a name="line681"></a>/**
<a name="line682"></a> * Callback that wraps receive_ to be used in timers.
<a name="line683"></a> * @type {Function}
<a name="line684"></a> * @private
<a name="line685"></a> */
<a name="line686"></a>goog.net.xpc.IframePollingTransport.receiveCb_ = goog.bind(
<a name="line687"></a>    goog.net.xpc.IframePollingTransport.receive_,
<a name="line688"></a>    goog.net.xpc.IframePollingTransport);
<a name="line689"></a>
<a name="line690"></a>
<a name="line691"></a>/**
<a name="line692"></a> * Starts the polling loop.
<a name="line693"></a> * @private
<a name="line694"></a> */
<a name="line695"></a>goog.net.xpc.IframePollingTransport.startRcvTimer_ = function() {
<a name="line696"></a>  goog.net.xpc.logger.fine(&#39;starting receive-timer&#39;);
<a name="line697"></a>  goog.net.xpc.IframePollingTransport.lastActivity_ = goog.now();
<a name="line698"></a>  if (goog.net.xpc.IframePollingTransport.rcvTimer_) {
<a name="line699"></a>    window.clearTimeout(goog.net.xpc.IframePollingTransport.rcvTimer_);
<a name="line700"></a>  }
<a name="line701"></a>  goog.net.xpc.IframePollingTransport.rcvTimer_ = window.setTimeout(
<a name="line702"></a>      goog.net.xpc.IframePollingTransport.receiveCb_,
<a name="line703"></a>      goog.net.xpc.IframePollingTransport.TIME_POLL_SHORT_);
<a name="line704"></a>};
<a name="line705"></a>
<a name="line706"></a>
<a name="line707"></a>
<a name="line708"></a>/**
<a name="line709"></a> * goog.net.xpc.IframePollingTransport.Sender
<a name="line710"></a> *
<a name="line711"></a> * Utility class to send message-parts to a document from a different origin.
<a name="line712"></a> *
<a name="line713"></a> * @constructor
<a name="line714"></a> * @param {string} url The url the other document will use for polling.
<a name="line715"></a> * @param {Object} windowObj The frame used for sending information to.
<a name="line716"></a> */
<a name="line717"></a>goog.net.xpc.IframePollingTransport.Sender = function(url, windowObj) {
<a name="line718"></a>  /**
<a name="line719"></a>   * The URI used to sending messages.
<a name="line720"></a>   * @type {string}
<a name="line721"></a>   * @private
<a name="line722"></a>   */
<a name="line723"></a>  this.sendUri_ = url;
<a name="line724"></a>
<a name="line725"></a>  /**
<a name="line726"></a>   * The window object of the iframe used to send messages.
<a name="line727"></a>   * The script instantiating the Sender won&#39;t have access to
<a name="line728"></a>   * the content of sendFrame_.
<a name="line729"></a>   * @type {Object}
<a name="line730"></a>   * @private
<a name="line731"></a>   */
<a name="line732"></a>  this.sendFrame_ = windowObj;
<a name="line733"></a>
<a name="line734"></a>  /**
<a name="line735"></a>   * Cycle counter (used to make sure that sending two identical messages sent
<a name="line736"></a>   * in direct succession can be recognized as such by the receiver).
<a name="line737"></a>   * @type {number}
<a name="line738"></a>   * @private
<a name="line739"></a>   */
<a name="line740"></a>  this.cycle_ = 0;
<a name="line741"></a>};
<a name="line742"></a>
<a name="line743"></a>
<a name="line744"></a>/**
<a name="line745"></a> * Sends a message-part (frame) to the peer.
<a name="line746"></a> * The message-part is encoded and put in the fragment identifier
<a name="line747"></a> * of the URL used for sending (and belongs to the origin/domain of the peer).
<a name="line748"></a> * @param {string} payload The message to send.
<a name="line749"></a> */
<a name="line750"></a>goog.net.xpc.IframePollingTransport.Sender.prototype.send = function(payload) {
<a name="line751"></a>  this.cycle_ = ++this.cycle_ % 2;
<a name="line752"></a>
<a name="line753"></a>  var url = this.sendUri_ + &#39;#&#39; + this.cycle_ + encodeURIComponent(payload);
<a name="line754"></a>
<a name="line755"></a>  // TODO Find out if try/catch is still needed
<a name="line756"></a>  /** @preserveTry */
<a name="line757"></a>  try {
<a name="line758"></a>    // safari doesn&#39;t allow to call location.replace()
<a name="line759"></a>    if (goog.userAgent.WEBKIT) {
<a name="line760"></a>      this.sendFrame_.location.href = url;
<a name="line761"></a>    } else {
<a name="line762"></a>      this.sendFrame_.location.replace(url);
<a name="line763"></a>    }
<a name="line764"></a>  } catch (e) {
<a name="line765"></a>    goog.net.xpc.logger.severe(&#39;sending failed&#39;, e);
<a name="line766"></a>  }
<a name="line767"></a>
<a name="line768"></a>  // Restart receiver timer on short polling interval, to support use-cases
<a name="line769"></a>  // where we need to capture responses quickly.
<a name="line770"></a>  goog.net.xpc.IframePollingTransport.startRcvTimer_();
<a name="line771"></a>};
<a name="line772"></a>
<a name="line773"></a>
<a name="line774"></a>
<a name="line775"></a>
<a name="line776"></a>/**
<a name="line777"></a> * goog.net.xpc.IframePollingTransport.Receiver
<a name="line778"></a> *
<a name="line779"></a> * @constructor
<a name="line780"></a> * @param {goog.net.xpc.Transport} transport The transport to receive from.
<a name="line781"></a> * @param {Object} windowObj The window-object to poll for location-changes.
<a name="line782"></a> * @param {Function} callback The callback-function to be called when
<a name="line783"></a> *     location has changed.
<a name="line784"></a> */
<a name="line785"></a>goog.net.xpc.IframePollingTransport.Receiver = function(transport,
<a name="line786"></a>                                                        windowObj,
<a name="line787"></a>                                                        callback) {
<a name="line788"></a>
<a name="line789"></a>  this.transport_ = transport;
<a name="line790"></a>  this.rcvFrame_ = windowObj;
<a name="line791"></a>
<a name="line792"></a>  this.cb_ = callback;
<a name="line793"></a>
<a name="line794"></a>  this.currentLoc_ = this.rcvFrame_.location.href.split(&#39;#&#39;)[0] + &#39;#INITIAL&#39;;
<a name="line795"></a>
<a name="line796"></a>  goog.net.xpc.IframePollingTransport.receivers_.push(this);
<a name="line797"></a>  goog.net.xpc.IframePollingTransport.startRcvTimer_();
<a name="line798"></a>};
<a name="line799"></a>
<a name="line800"></a>
<a name="line801"></a>/**
<a name="line802"></a> * Polls the location of the receiver-frame for changes.
<a name="line803"></a> * @return {boolean} Whether a change has been detected.
<a name="line804"></a> */
<a name="line805"></a>goog.net.xpc.IframePollingTransport.Receiver.prototype.receive = function() {
<a name="line806"></a>  var loc = this.rcvFrame_.location.href;
<a name="line807"></a>
<a name="line808"></a>  if (loc != this.currentLoc_) {
<a name="line809"></a>    this.currentLoc_ = loc;
<a name="line810"></a>    var payload = loc.split(&#39;#&#39;)[1];
<a name="line811"></a>    if (payload) {
<a name="line812"></a>      payload = payload.substr(1); // discard first character (cycle)
<a name="line813"></a>      this.cb_(decodeURIComponent(payload));
<a name="line814"></a>    }
<a name="line815"></a>    return true;
<a name="line816"></a>  } else {
<a name="line817"></a>    return false;
<a name="line818"></a>  }
<a name="line819"></a>};
</pre>


</body>
</html>
